// pages/group/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    page:1,
    loading:0,
    lastpage:0,
    groupData:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // console.log(options);
    var window = app.globalData.window;
    window.windowHeight = window.windowHeight-30;
    this.setData({ window: window, id: options.id})
    //this.setData({ window: window, id: 88 })
    this.getGroup();
    this.getTopicList();
  },

  //
  getGroup:function(){
    var that = this;
    var id = this.data.id;
    app.api("group/find", {
      id: id,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      wx.setNavigationBarTitle({
        title: res.data.value
      });
      that.setData({ groupData: res.data });
    })
  },


  //api evens/list 活动列表 
  getTopicList:function(reset = 0) {
    var that = this;
   // var order_by = that.data.showOrder;
    //重新加载 
    if (reset == 1) {
      that.setData({ page: 1, listData: [], loading: 0, lastpage:0 });
      app.setPageData("group/index", { reload: 0 });
    }
    if (this.data.loading == 1 || this.data.lastpage==1){
      return false;
    }
    this.data.loading=1;
   //   lastpage:0,
    var page = that.data.page;
    //console.log(event_type);
    app.api("topic/list", {
      page: page,
      status:1,
      pid:this.data.id,
      type:'todolist',
      order:'id desc',
      limit:15,
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length==0){
        that.data.lastpage=1;
      }
      page++;
      that.setData({ page:page,listData: that.data.listData.concat(res.data.list) });
      
    })
  },
  thumb_up: function (e) {//列表点赞 
    var that = this;
    var index = e.currentTarget.dataset.id;
    var topic_id = e.currentTarget.dataset.topic_id;
    app.thumb_up(topic_id, function (res) {
      var topicData = that.data.listData[index];
      if (res.type == "add") {
        topicData.good_count++;
        topicData.thumb_up_ed = 1;
      } else if (res.type == "del") {
        topicData.good_count--;
        topicData.thumb_up_ed = 2;
      }
      that.data.listData[index] = topicData;
      that.setData({
        listData: that.data.listData
      });
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var reload = app.getPageData("group/index", 'reload');
    if (reload==1){
      this.getTopicList(1);
      this.getGroup();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },


  //下拉刷新
  onPullDownRefresh: function () {
    this.getTopicList(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getTopicList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  scrolltolower: function (e) {
    console.log(e);
  },
  apply1: function () {
    wx.navigateTo({
      url: '/pages/topic/issue?pid=' + this.data.id
    })
  },
  //浏览图片
  previewListImage: function (e) {
    var current = e.target.dataset.src;
    var index = e.target.dataset.index;
    var pics = this.data.listData[index].pics;
    wx.previewImage({
      current: current,
      urls: pics
    })
  },
})